//
//  KCRVC.m
//  Golden
//
//  Created by king on 2022/7/27.
//  Copyright © 2022 KC. All rights reserved.
//

#import "KCKit.h"
#import "KCRVC.h"
#import "KCRLVC.h"
#import "KCRMVC.h"
#import "KCRNVC.h"
#import "KCStation.h"
#import "KCTreasureModel.h"
#import "KCTreasureCell.h"
#import "KCTreasureDetailVC.h"

@interface KCRVC ()<KCStationDelegate>

@property (nonatomic,strong)KCStation *station;
@property (nonatomic,strong)UILabel *titLabel;
@property (nonatomic,assign)NSInteger theIndex;
@property (nonatomic,strong)UIView *downView;
@property (nonatomic,strong)UIImageView *mmm;

@end

@implementation KCRVC

- (KCStation *)station {
    if (!_station) {
        _station = [KCStation RegisterStation:KHttpStationHome delegate:self];
    }
    return _station;
}

- (id)dispatch:(KCCargo *)cargo {
    
    if (cargo.cargoID == 0x01) {
        
    }
    return nil;
    
}

#pragma mark - LifeCycle

- (void)viewDidLoad {
    [super viewDidLoad];
    _theIndex = 0;
    self.KCNavView.hidden = YES;
    _titLabel = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - 100, 60, 80, 200)];
    _titLabel.numberOfLines = 0;
    _titLabel.textColor = WHITE_COLOR;
    _titLabel.font = SYSTEM_FONT(60);
    [self.view addSubview:_titLabel];
    
    //手势
    UISwipeGestureRecognizer *swipeGes = [[UISwipeGestureRecognizer alloc] init];
    swipeGes.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.view addGestureRecognizer:swipeGes];
    [swipeGes addTarget:self action:@selector(swipe:)];
    
    UISwipeGestureRecognizer *swipeGes2 = [[UISwipeGestureRecognizer alloc] init];
    swipeGes2.direction = UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:swipeGes2];
    [swipeGes2 addTarget:self action:@selector(swipe:)];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction)];
    [self.view addGestureRecognizer:tap];

    //加载数据
    [self refreshData];
    //
    
    _downView = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 90, SCREEN_WIDTH, 90)];
    _downView.backgroundColor = R_G_B_A_COLOR(123, 123, 123, 0.3);
    [self.view addSubview:_downView];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:@"全部颜色" forState:UIControlStateNormal];
    btn.titleLabel.font = SYSTEM_BOLD_FONT(15);
    btn.frame = CGRectMake(0, 0, SCREEN_WIDTH/3, 60);
    [btn addTarget:self action:@selector(aAction)forControlEvents:UIControlEventTouchUpInside];
    [_downView addSubview:btn];
    
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn2 setTitle:@"相关藏品" forState:UIControlStateNormal];
    btn2.titleLabel.font = SYSTEM_BOLD_FONT(15);
    btn2.frame = CGRectMake(SCREEN_WIDTH/3, 0, SCREEN_WIDTH/3, 60);
    [btn2 addTarget:self action:@selector(bAction)forControlEvents:UIControlEventTouchUpInside];
    [_downView addSubview:btn2];
    
    UIButton *btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn3 setTitle:@"题材介绍" forState:UIControlStateNormal];
    btn3.titleLabel.font = SYSTEM_BOLD_FONT(15);
    btn3.frame = CGRectMake(SCREEN_WIDTH/3 * 2, 0, SCREEN_WIDTH/3, 60);
    [btn3 addTarget:self action:@selector(cAction)forControlEvents:UIControlEventTouchUpInside];
    [_downView addSubview:btn3];
    
    if ([DEFAULTS boolForKey:@"mmm"]) {
        KCLog(@"已经展示过");
    }else {
        _mmm = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
        _mmm.image = [UIImage imageNamed:@"lefth"];
        _mmm.center = self.view.center;
        [self.view addSubview:_mmm];
        
        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];//必须写opacity才行。
        animation.fromValue = [NSNumber numberWithFloat:1.0f];
        animation.toValue = [NSNumber numberWithFloat:0.0f];//这是透明度。
        animation.autoreverses = YES;
        animation.duration = 1.2;
        animation.repeatCount = 3;
        animation.removedOnCompletion = YES;
        animation.fillMode = kCAFillModeRemoved;
    //    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];///没有的话是均匀的动画。
        [_mmm.layer addAnimation:animation forKey:@"aaa"];

        [self performSelector:@selector(hihi) afterDelay:4];
    }
    

    
}

#pragma mark -Actions

- (void)hihi {
    _mmm.hidden = YES;
    [DEFAULTS setBool:1 forKey:@"mmm"];
}

- (void)tapAction {
    WEAK_SELF;
    if (_downView.top == SCREEN_HEIGHT) {
        [UIView animateWithDuration:0.3 animations:^{
            weakSelf.downView.top = SCREEN_HEIGHT - 90;
        }];
    }else {
        [UIView animateWithDuration:0.3 animations:^{
            weakSelf.downView.top = SCREEN_HEIGHT;
        }];
    }
}
- (void)aAction {
    KCRLVC *lvc = [[KCRLVC alloc]init];
    [self.navigationController pushViewController:lvc animated:YES];
}
- (void)bAction {
    KCRMVC *mvc = [[KCRMVC alloc]init];
    mvc.IDString = [KCSingle shareInstance].nameArray[_theIndex];
    [self.navigationController pushViewController:mvc animated:YES];
}
- (void)cAction {
    KCRNVC *nvc = [[KCRNVC alloc]init];
    nvc.IDString = [KCSingle shareInstance].nameArray[_theIndex];
    [self.navigationController pushViewController:nvc animated:YES];
}

-(void)swipe:(UISwipeGestureRecognizer*) swipe {
    
    //反馈震感
    UIImpactFeedbackGenerator *feedBackGenertor = [[UIImpactFeedbackGenerator alloc] initWithStyle:UIImpactFeedbackStyleMedium];
    [feedBackGenertor impactOccurred];
    //下标
    if(swipe.direction == UISwipeGestureRecognizerDirectionLeft){
        _theIndex ++;
    }else if(swipe.direction == UISwipeGestureRecognizerDirectionRight) {
        _theIndex --;
    }
    if (_theIndex == [KCSingle shareInstance].nameArray.count) {
        _theIndex = 0;
    }else if (_theIndex < 0) {
        _theIndex = ([KCSingle shareInstance].nameArray.count - 1);
    }
    //刷新页面
    [self refreshData];
    //隐藏底部
    WEAK_SELF;
    [UIView animateWithDuration:0.3 animations:^{
        weakSelf.downView.top = SCREEN_HEIGHT;
    }];
}

- (void)refreshData {
    //颜色名称
    _titLabel.text = [KCSingle shareInstance].nameArray[_theIndex];
    self.view.backgroundColor = [KCSingle shareInstance].colorArray[_theIndex];
    //特殊判断
    if ([_titLabel.text isEqualToString:@"白"]) {
        _titLabel.textColor = [UIColor blackColor];
    }else {
        _titLabel.textColor = WHITE_COLOR;
    }
}

@end
